package parallel;
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueLineRecordReader;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.LineRecordReader;
import org.apache.hadoop.mapreduce.RecordReader;

/**
 * An input format that reads the first 10 characters of each line as the key
 * and the rest of the line as the value. Both key and value are represented
 * as Text.
 */
public class EventInputFormat extends FileInputFormat<LongWritable,Text> {

	private static List<InputSplit> splits = null;

	static class EventRecordReader extends RecordReader<LongWritable,Text>{
		private KeyValueLineRecordReader in;
		private LongWritable key = new LongWritable();
		private Text line = new Text();

		public EventRecordReader(Configuration conf) throws IOException {
			in = new KeyValueLineRecordReader(conf);
		}

		public void close() throws IOException {
			in.close();
		}
		public float getProgress() throws IOException {
			return in.getProgress();
		}

		@Override
		public LongWritable getCurrentKey() throws IOException,
		InterruptedException {
			// TODO Auto-generated method stub
			return new LongWritable(key.get());
		}

		@Override
		public Text getCurrentValue() throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			return new Text(line);
		}

		@Override
		public void initialize(InputSplit inputSplit, TaskAttemptContext context)
				throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			in.initialize(inputSplit, context);
		}

		@Override
		public boolean nextKeyValue() throws IOException, InterruptedException {
			// TODO Auto-generated method stub
			if (in.nextKeyValue()) {
				long key = Long.parseLong(in.getCurrentKey().toString());
				this.key.set(key);
				line.set(in.getCurrentValue().toString());
				return true;
			} else {
				return false;
			}
		}
	}

	@Override
	public List<InputSplit> getSplits(JobContext context) throws IOException {
		splits = super.getSplits(context);
		return splits;
	}


	@Override
	public RecordReader<LongWritable, Text> createRecordReader(InputSplit inputSplit,
			TaskAttemptContext context) throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		EventRecordReader reader = new EventRecordReader(context.getConfiguration());
		reader.initialize(inputSplit, context);
		return reader;
	}
}
